package com.example.hp.qingyi.scenic.fragment;

import android.annotation.SuppressLint;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.hp.qingyi.R;
import com.example.hp.qingyi.base.BaseFragment;
import com.example.hp.qingyi.scenic.fragment.adapter.LocationAdapter;
import com.example.hp.qingyi.scenic.fragment.adapter.RecommendAdapterOne;
import com.example.hp.qingyi.scenic.fragment.adapter.StrategyAdapter;
import com.example.hp.qingyi.scenic.fragment.bean.LocationBean;
import com.example.hp.qingyi.scenic.fragment.bean.RecommendBean;
import com.example.hp.qingyi.scenic.fragment.bean.StrategyBean;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;


/**
 * 旅行地页面
 */
public class ScenicFragment extends BaseFragment {
    //推荐网址
    private String url="http://www.iqingyi.com/scenic/get_theme_scenics";
    //周边旅游地网址
    private  String url2="http://www.iqingyi.com/scenic/get_nearby_scenics_by_location?longitude=106.577742&latitude=26.603974";
    //周边攻略
    private String url3="http://www.iqingyi.com/scenic/get_contents_of_nearby_scenics_by_location?longitude=106.577742&latitude=26.603974";
    //搜索
    private String ss="http://www.iqingyi.com/search/suggestion?keyword=gg&size=10&type=1";
    private TextView one_recommend_tv,two_recommend_tv;
    private RecyclerView recommend_one_rv,recommend_two_rv,location_rv,strategy_rv;
    private EditText search_et;
    @Override
    public View initView() {
        View inflate = LayoutInflater.from(context).inflate(R.layout.fragmetn_travel, null);
        one_recommend_tv=inflate.findViewById(R.id.one_recommend_tv);
        two_recommend_tv=inflate.findViewById(R.id.two_recommend_tv);
        recommend_one_rv=inflate.findViewById(R.id.recommend_one_rv);
        recommend_two_rv=inflate.findViewById(R.id.recommend_two_rv);
        location_rv=inflate.findViewById(R.id.location_rv);
        strategy_rv=inflate.findViewById(R.id.strategy_rv);
        search_et=inflate.findViewById(R.id.search_et);
        recommend_one_rv.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayout.HORIZONTAL,false));
        recommend_two_rv.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayout.HORIZONTAL,false));
        location_rv.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayout.HORIZONTAL,false));
        strategy_rv.setLayoutManager(new LinearLayoutManager(getContext()));
        //加载推荐信息
        OkHttpUtils.get()
                .url(url)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.e("CG",response);
                        request(response);
                    }
                });
        //加载周边旅行地
        OkHttpUtils.get()
                .url(url2)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(String response, int id) {
                        LocationBean locationBean = new Gson().fromJson(response, LocationBean.class);
                        LocationAdapter adapter=new LocationAdapter(getContext(),locationBean);
                        location_rv.setAdapter(adapter);

                    }
                });
        //周边攻略
        OkHttpUtils.get()
                .url(url3)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(String response, int id) {
                        StrategyBean strategyBean = new Gson().fromJson(response, StrategyBean.class);
                        List<StrategyBean.DataBean.ListBean> list = strategyBean.getData().get(0).getList();
                        StrategyAdapter strategyAdapter=new StrategyAdapter(getContext(),list);
                        strategy_rv.setAdapter(strategyAdapter);
                    }
                });
        //搜索
        OkHttpUtils.get()
                .url("http://www.iqingyi.com/search/suggestion?keyword="+search_et.getText()+"&size=10&type=1")
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(String response, int id) {
                        
                    }
                });

        return inflate;
    }

    @Override
    public void initData() {
        super.initData();
        Log.i("initData","旅行地");

    }

    private void request(String response) {
        RecommendBean recommendBean = new Gson().fromJson(response, RecommendBean.class);
        String theme = recommendBean.getData().getScenics().get(0).getTheme();
        one_recommend_tv.setText(theme);

        String theme2 = recommendBean.getData().getScenics().get(1).getTheme();
        two_recommend_tv.setText(theme2);
        //推荐1
        List<RecommendBean.DataBean.ScenicsBeanX.ScenicsBean> scenics = recommendBean.getData().getScenics().get(0).getScenics();
        RecommendAdapterOne adapterOne=new RecommendAdapterOne(context,scenics);
        recommend_one_rv.setAdapter(adapterOne);
        //推荐2
        List<RecommendBean.DataBean.ScenicsBeanX.ScenicsBean> scenics2 = recommendBean.getData().getScenics().get(1).getScenics();
        RecommendAdapterOne adapterTwo=new RecommendAdapterOne(context,scenics2);
        recommend_two_rv.setAdapter(adapterTwo);
    }

}
